package edu.zut.hb.software.student.controller;

import com.alibaba.fastjson.JSON;
import edu.zut.hb.software.base.utils.R;
import edu.zut.hb.software.base.web.spring.controller.GenericController;
import edu.zut.hb.software.student.domain.Student;
import edu.zut.hb.software.student.service.StudentService;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping(value = "/student", produces = { MediaType.APPLICATION_JSON_VALUE })
public class StudentController extends GenericController<Student, Long, StudentService> {

    public static final Logger logger = LogManager.getLogger(StudentController.class.getName());
    StudentService studentService;

    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
        this.manager=studentService;
    }
    @ResponseBody
    @RequestMapping(value="/findAll/{page}/{pageSize}",method = {RequestMethod.GET,RequestMethod.POST})
    public R findAllStudent(@PathVariable("page") int page, @PathVariable("pageSize") int pageSize){
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        Pageable pageable = PageRequest.of(page,pageSize, sort);

        Page<Student> list = studentService.findAll(pageable);

        HashMap<String,Object> map = new HashMap<>();
        map.put("number",list.getTotalElements());
        map.put("userList",list.getContent());
        logger.info("----------->"+list.getContent());
        //map集合转化为JSON字符串进行数据传输
//        String res = JSON.toJSONString(map);
        return R.ok().data(map);
    }

}
